<?php

namespace App\Http\Controllers;

use App\Activity;
use App\Datatables;
use Illuminate\Http\Request;

class ActivityController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * 活动列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function activityList(Request $request)
    {
        //datatables
        if ($request->isMethod('post')) {
            return response()->json(Datatables::ActivityList());
        }

        return view('activity.activityList');
    }

    /**
     * 添加活动
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
     */
    public function activityPublish(Request $request)
    {
        if ($request->isMethod('post')) {
            //开始时间应该晚于结束时间
            $start_time = strtotime($request->start_time . ':00');
            $end_time = strtotime($request->end_time . ':00');

            if ($start_time > $end_time) {
                return ['status' => 2, 'message' => '开始时间应该早于结束时间'];//back()->with('status', 2)->with('message', '开始时间应该早于结束时间');
            }
            return Activity::ActivityPublish($request, $start_time, $end_time);
        }

        return view('activity.activityPublish');
    }

    /**
     * 修改活动
     * @param Request $request
     * @param Activity $activity
     * @return $this|\Illuminate\Http\RedirectResponse
     */
    public function activityModify(Request $request, Activity $activity)
    {

        if ($request->isMethod('post')) {
            //开始时间应该晚于结束时间
            $start_time = strtotime($request->start_time . ':00');
            $end_time = strtotime($request->end_time . ':00');

            if ($start_time > $end_time) {
                return ['status' => 2, 'message' => '开始时间应该早于结束时间'];
            }
            return Activity::ActivityModify($request, $activity->id, $start_time, $end_time);
//            return back()->with('status', $return['status'])->with('message', $return['message']);
        }
//        $start_time = dateToArr($activity->start_time);
//        $end_time = dateToArr($activity->end_time);
        return view('activity.activityModify')->with(compact('activity'));
    }


    /**
     * 删除
     * @param Activity $activity
     * @return array
     * @throws \Exception
     */
    public function activityDel(Activity $activity)
    {
        if (!$activity->delete()) {
            return ['status' => 2, 'message' => '删除失败'];
        }
        return ['status' => 1, 'message' => '删除成功'];
    }
}
